package adapter.role {
	import datapool.global.GlobalInfo;
	import adapter.common.AdapterBase;
	
	import datapool.Context;
	import datapool.define.ActionDefine;
	import datapool.define.DirectionDefine;
	import datapool.model.ModelInfo;
	
	import flash.display.MovieClip;
	import flash.geom.Point;
	
	import ui.role.RoleBaseUI;

	/**
	 * @author Administrator
	 */
	public class RoleAdapterBase extends AdapterBase
	{
		private var _playerId:uint;
		
		protected var _speed:int = 5;
		protected var _targetPos:Point;
		protected var _postion : Point;
		protected var _dir : String = DirectionDefine.DIR_Y_X;
		protected var _action : String = ActionDefine.ACTION_D_J;
		private var _roleMC:MovieClip;

		private var _uiReady:Boolean   = false;
		private var _dataReady:Boolean = false;
		
		public function RoleAdapterBase(modelId:uint)
		{
			super();
			init(modelId);
		}
		
		private function init(modelId:uint):void
		{
			_playerId  = modelId;
			_postion   = new Point(300, 300);
			var model:ModelInfo = context.modelPool.getModelInfoById(modelId);
			if(model)
			{
				context.remoteResource.requestRs(model.modelUrl, onComplete);
			}
		}
		
		public function onComplete(data:*):void
		{
			_roleMC = data as MovieClip;
			if(_roleMC)
			{
				_dataReady = true;
				checkAndUpdate();
			}
		}
		
		public function get roleUI():RoleBaseUI
		{
			return ui as RoleBaseUI;
		}
		
		public function onFrame():void
		{			
			if(roleUI)
			{
				roleUI.oneTick();
			}
		}

		override protected function onUIAttach():void
		{
			super.onUIAttach();
			_uiReady = true;
			checkAndUpdate();
		}
		
		override protected function onUIDeattach():void
		{
			_uiReady = false;
			super.onUIDeattach();
		}
		
		private function checkAndUpdate():void
		{
			if(_uiReady && _dataReady)
			{
				if(roleUI)
				{
					refreshNotify();
					roleUI.updatePostion(_postion);
				}
			}
		}
		
		public function set action(act:String):void
		{
			_action = act;
			refreshNotify();
		}
		
		public function set direction(dir:String):void
		{
			_dir = dir;
			refreshNotify();
		}
		
		public function set targetPostion(point:Point):void
		{
			if(point)
			{
				_targetPos = point;
				var dir : String = DirectionDefine.caculateDirection(new Point(absPostionX, absPostionY), _targetPos);
				if (dir != DirectionDefine.DIR_ERR)
				{
					action = ActionDefine.ACTION_Z_L;
					direction = dir;
				}
			}
		}
		
		public function get targetPostion():Point
		{
			return _targetPos;
		}
		
		//更新角色本身的位置
		public function set postion(pos:Point):void
		{
			if(pos)
			{
				_postion = pos;
			}
			updatePos(_postion);
		}
		
		//输入：绝对位置 并更新显示
		public function updatePos(pos:Point):void
		{
			if(roleUI)
			{
				roleUI.updatePostion(pos);
			}
		}
		
		public function get postion():Point
		{
			return _postion;
		}
		
		public function get absPostionX():int
		{
			return _postion.x + GlobalInfo.oppositePosX;
		}
		
		public function get absPostionY():int
		{
			return _postion.y + GlobalInfo.oppositePosY;
		}
		
		public function get speed():int
		{
			return _speed;
		}
		
		public function get bmdArr():Array
		{
			if(_roleMC)
			{
				var name:String = _roleMC["roleName"];
				var obj:Object 	= _roleMC[name + "_" + _action + "_" + _dir];
				if( obj )
				{			
					return obj.imageArray;
				}
			}
			return null;
		}
		
		public function get playerId():uint
		{
			return _playerId;
		}
		
		public function get isMainRole():Boolean
		{
			return false;
		}
	}
}
